package com.easykoo.web.tag;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.*;

public class PageInfo implements Serializable {
    private static final long serialVersionUID = -2021715257430889833L;

    public static final String IMAGE_PATH = "image_path";
    public static final String IMAGE_COLOR = "image_color";
    public static final String PAGE_SIZE = "page_size";
    public static final String TYPE_IMG = "img";
    public static String FIRST_PAGE = "first_page";
    public static String PRE_PAGE = "pre_page";
    public static String NEXT_PAGE = "next_page";
    public static String LAST_PAGE = "last_page";
    public static String GO_TO = "go_to";
    public static String TOTAL_FOUND = "total_found";
    public static String NOT_FOUND = "not_found";

    private List result;
    private Integer totalFound;
    private Integer currPage = 1;
    private Integer prePage;
    private Integer nextPage;
    private Integer pageSize;
    private Integer totalPage;
    private String orderBy;
    private Map<String, String> searchCriteria;
    private boolean asc;
    private String imagePath;
    private String imageColor;
    private ResourceBundle config;
    private int min;
    private int max;


    public void init() {
        try {
            config = ResourceBundle.getBundle("easy-pager");
            pageSize = Integer.parseInt(config.getString(PAGE_SIZE));
            imagePath = config.getString(IMAGE_PATH);
            imageColor = config.getString(IMAGE_COLOR);
            FIRST_PAGE = config.getString(FIRST_PAGE);
            PRE_PAGE = config.getString(PRE_PAGE);
            NEXT_PAGE = config.getString(NEXT_PAGE);
            LAST_PAGE = config.getString(LAST_PAGE);
            GO_TO = config.getString(GO_TO);
            TOTAL_FOUND = config.getString(TOTAL_FOUND);
            NOT_FOUND = config.getString(NOT_FOUND);
        } catch (MissingResourceException e) {
            if (BaseTag.isBlank(imagePath)) {
                imagePath = "/images/";
            }
            if (BaseTag.isBlank(imageColor)) {
                imageColor = "blue";
            }
        }

        if (pageSize == null || pageSize == 0) {
            pageSize = 10;
        }
        currPage = 1;
        totalFound = 0;
        asc = true;
    }

    public static PageInfo getPageInfo(HttpServletRequest req, String pageInfoName) {
        PageInfo pageInfo = (PageInfo) req.getSession().getAttribute(pageInfoName);

        if (pageInfo == null) {
            pageInfo = new PageInfo();
        }
        String page = req.getParameter("page");
        if (BaseTag.isNotBlank(page)) {
            pageInfo.setCurrPage(Integer.decode(page));
        }

        String orderBy = req.getParameter("orderBy");
        pageInfo.setOrderBy(orderBy);

        String asc = req.getParameter("asc");
        pageInfo.setAsc("1".equals(asc) ? true : false);

        return pageInfo;
    }

    public boolean hasPrevious() {
        return currPage > 1;
    }

    public boolean hasNext() {
        return currPage < totalPage;
    }

    public int getMin() {
        return ((this.currPage - 1) * this.getPageSize());
    }

    public void setMin(int min) {
        this.min = min;
    }

    public int getMax() {
        return this.currPage * this.pageSize;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public PageInfo() {
        init();
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getCurrPage() {
        return currPage;
    }

    public void setCurrPage(Integer currPage) {
        if (currPage <= 0) {
            currPage = 1;
        } else if (currPage >= totalPage) {
            currPage = totalPage;
        }
        this.currPage = currPage;
        this.min = ((this.currPage - 1) * this.pageSize);
        this.max = this.currPage * this.pageSize;
    }

    public Integer getPrePage() {
        if (currPage != 0) {
            return currPage - 1;
        } else return 1;
    }

    public void setPrePage(Integer prePage) {
        this.prePage = prePage;
    }

    public Integer getNextPage() {
        if (currPage < totalPage) {
            return currPage + 1;
        } else {
            return currPage;
        }
    }

    public void setNextPage(Integer nextPage) {
        this.nextPage = nextPage;
    }

    public PageInfo(List result, Integer totalFound) {
        this.result = result;
        this.totalFound = totalFound;
    }

    public void setResult(List result) {
        this.result = result;
    }

    public void setTotalFound(Integer totalFound) {
        this.totalFound = totalFound;
        if (totalFound % pageSize == 0) {
            this.totalPage = totalFound / pageSize;
        } else {
            this.totalPage = totalFound / pageSize + 1;
        }
    }

    public List getResult() {
        return this.result;
    }

    public Integer getTotalFound() {
        return this.totalFound;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public boolean isAsc() {
        return asc;
    }

    public void setAsc(boolean asc) {
        this.asc = asc;
    }

    public Map<String, String> getSearchCriteria() {
        return searchCriteria;
    }

    public void setSearchCriteria(Map<String, String> searchCriteria) {
        this.searchCriteria = searchCriteria;
    }

    public void putCriteria(String attr, String value) {
        if (searchCriteria == null) {
            searchCriteria = new HashMap<String, String>();
        }
        searchCriteria.put(attr, value);
    }

    public String getCriteria(String attr) {
        if (searchCriteria != null) {
            return searchCriteria.get(attr);
        }
        return null;
    }

    public ResourceBundle getConfig() {
        return config;
    }

    public void setConfig(ResourceBundle config) {
        this.config = config;
    }

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public String getImageColor() {
        return imageColor;
    }

    public void setImageColor(String imageColor) {
        this.imageColor = imageColor;
    }
}